import {action, computed, observable} from "mobx";
import {findModel, getComponentConfig, getTables, menuList} from '../../../../api/work/GenerateApi'

class Store {
    // 常规页面
    ordinary = 1;
    // 简单页面
    simple = 2;
    // 数值页面
    number = 3;
    // 子菜单 key map
    subTypeMap = {
        1:"ordinary",
        2:"simple",
        3:"number",
    };
    //子菜单类型
    @observable subType = null;
    // 展开的菜单
    @observable openKeys = [];
    // 子菜单列表
    @observable menuList = [];
    // 分组
    @observable group = null;
    // 模块
    @observable module = null;
    // 控制器
    @observable controller = null;
    // 弹窗控制
    @observable menuVisible = false;
    // PID
    @observable pid;

    //###################################
        //常规页面配置tab激活的key
        //两个常量
        //通过配置生成
        CONFIG_TYPE = "1";
        //通过数据表生成
        CUSTOM_TYPE = "2";
        //简单页面类型
        SIMPLE = "3";
        //数值页面
        NUMBER = "4";
        @observable activeKey = "model";
        //模型初始化信息
        @observable modelInfo = {};
        //生成模型的tab类型modelType
        @observable modelType = "1";
        // 模型初始数据
        @observable modalData = [];
        //模型初始数据map 用户后期的数据更改使用
        @observable modalDataMap = {};
        //配置默认的总条数
        @observable count=0;
        //所有的数据表
        @observable allTables = [];
    //###################################
    //###################################
    //页面配置相关
        //当前步骤
        @observable stepCurrent = 4;
        //功能选择-选中的列表
        @observable checkedList = [];
        //组件配置-dataSource
        @observable componentDataSource = [];
        //组件配置-dataSourceMap
        @observable componentDataSourceMap = {};
        //首页配置
        // 字段-描述的map
        @observable fieldMap = {};
        //表格展示的字段
        @observable table_field_display = [];
        //可搜索的字段
        @observable search_field_display = [];
        //可导入导出的字段
        @observable im_ex_port_field_display = [];
        //导入导出模板名称
        @observable export_template_filename = "";
        //提示信息
        @observable hintMessage = {};
    //###################################
    //页面生成信息
    @observable processList = [];
    @observable status;
    @observable title;
    @observable subTitle;

    @observable resultVisible = false;

    //点击的菜单id
    @observable menuInfo={};
    @observable updateMenuVisible = false;


    // 设置子菜单类型
    @action setSubType = (subType) => {
       if (subType !== this.subType){ //菜单展开
           this.menuList = [];
           this.subType = subType;
           this.group = this.subTypeMap[subType];
           this.module = null;
           this.setOpenKeys();
           this.getMenuByType();
       }else { //菜单关闭
           this.openKeys.remove(this.group);
           this.subType = null;
       }
    };

    @action setModule = (module) => {
        if (this.module !== module){ //模块展开
            this.module = module;
            this.setOpenKeys();
        }else { //模块关闭
            this.openKeys.remove(this.module);
            this.module = null
        }
    };

    @action setController = (controller) => {
        this.controller = controller;
    };

    setOpenKeys = () => {
        this.openKeys = [];
        if (this.group){
            this.openKeys.push(this.group);
        }
        if (this.module){
            this.openKeys.push(this.module);
        }
        console.log(this.openKeys)
    };

    @action getMenuByType = () => {
        if (this.openKeys.length === 0){
            this.menuList = [];
        }else if (this.openKeys.length === 1){
            // type 固定为2
            menuList({type:2,sub_type:this.subType}).then(response => {
                if (response.data.code === 200){
                    this.menuList = response.data.list;
                }
            });
        }
    };

    @computed get pageRoute(){
        // return "test/test1";
        return this.module + "/" + this.controller;
    }

    // 刷新菜单
    @action flashMenu = () => {
        console.log("刷新菜单：",this.subType);
        menuList({type:2,sub_type:this.subType}).then(response => {
            if (response.data.code === 200){
                this.menuList = response.data.list;
            }
        });
    };

    @action fetchModelInfo = () => {
        //先初始化模型信息
        this.modelInfo = {};
        this.modalData = [];
        this.modelType = "1";
        this.count = 0;
        this.modalDataMap = {};
        findModel({"page":this.pageRoute}).then(response => {
            if (response.data.code === 200){
                this.modelInfo = response.data.modelInfo;
                this.modalData = response.data.data;
                this.modelType = response.data.type;
                this.count = this.modalData.length;
                this.modalData.map((item,index) => {
                    this.modalDataMap[index]= item;
                    return true;
                })
            }
        })
    };

    //获取页面配置信息
    @action fetchPageInfo = () => {
        this.fieldMap = {};
        this.componentDataSourceMap = {};
        this.componentDataSource = [];
        getComponentConfig({page:this.pageRoute}).then(response => {
            if (response.data.code === 200){
                //初始化参数
                let info = response.data.info;
                this.componentDataSourceMap = info.hasOwnProperty("component_config_map") ? info["component_config_map"] : {};
                this.componentDataSource = info.hasOwnProperty("component_config") ? info["component_config"] : [];
                this.table_field_display = info.hasOwnProperty("table_fields") ? info["table_fields"] : [];
                this.search_field_display = info.hasOwnProperty("search_fields") ? info["search_fields"] : [];
                this.im_ex_port_field_display = info.hasOwnProperty("im_ex_fields") ? info["im_ex_fields"] : [];
                this.export_template_filename = info.hasOwnProperty("template_filename") ? info["template_filename"] : "";
                // this.searchConfig = response.data.info["table_fields"];
                this.hintMessage = info.hasOwnProperty("hint_message") ? info["hint_message"] : {};
                this.fieldMap = info.hasOwnProperty("fieldMap") ? info["fieldMap"] : {};
                this.checkedList = info.hasOwnProperty("zone_list") ? info["zone_list"] : []
            }
        })
    };


    @action fetchAllTables = () => {
        getTables().then(response => {
            if (response.data.code === 200){
                this.allTables = response.data.list;
            }
        })
    };
}


export default new Store();
